<?php

namespace app\controller\manager;

use app\BaseController;
use \think\facade\Db;
use think\request;
use app\model\Academy;
use app\model\Major;
use app\Request as AppRequest;

/**
 * 学院专业控制器
 * @todo 完善增删改查
 * @todo 完善excel导入
 */
class AcademyMajorManage extends BaseController
{

    /**
     * 学院列表
     */
    public function list(Request $request)
    {
        if ($request->isGet()) {
            $academy = Academy::select()->toArray();
            $major = Major::select()->toArray();
            $data = [];
            foreach ($academy as $ac) {
                $ac['majorList'] = [];
                $data[$ac['academyID']] = $ac;
                $data[$ac['academyID']]['isLeaf'] = true;
            }
            foreach ($major as $mj) {
                $data[$mj['academyID']]['majorList'][] = $mj;
                $data[$mj['academyID']]['isLeaf'] = false;
            }
            // return json(
            //     array_values($data)
            // );
            return resultSuccess(array_values($data));
        } else {
            return resultError();
        }
    }

    /**
     * 增加学院
     */
    public function addAcademy(Request $request)
    {
        if ($request->isPost()) {
            $academy = new Academy;
            if ($academy->save(
                [
                    'academyName' => $request->param('academyName')
                ]
            )) {
                return resultSuccess();
            } else {
                return resultError();
            }
        } else {
            return resultError();
        }
    }

    /**
     * 删除学院
     */
    public function deleteAcademy(Request $request)
    {
        if ($request->isGet()) {
            $academy = Academy::find($request->param('academyID'));
            if ($academy->delete()) {
                return resultSuccess();
            } else {
                return resultError();
            }
        } else {
            return resultError();
        }
    }

    /**
     * 修改学院信息
     */
    public function editAcademy(Request $request)
    {
        if ($request->isPost()) {
            $academy = Academy::find($request->param('academyID'));
            $academy->academyName = $request->param('academyName');
            if ($academy->save()) {
                return resultSuccess();
            } else {
                return resultError();
            }
        } else {
            return resultError();
        }
    }

    /**
     * 增加专业
     */
    public function addMajor(Request $request){
        if ($request->isPost()) {
            $major = new Major;
            if ($major->save(
                [
                    'academyID' => $request->param('academyID'),
                    'majorName' => $request->param('majorName'),
                ]
            )) {
                return resultSuccess();
            } else {
                return resultError();
            }
        } else {
            return resultError();
        }
    }

    /**
     * 删除专业
     */
    public function deleteMajor(Request $request){
        if ($request->isGet()) {
            $major = Major::find($request->param('majorID'));
            if ($major->delete()) {
                return resultSuccess();
            } else {
                return resultError();
            }
        } else {
            return resultError();
        }
    }

    /**
     * 修改专业信息
     */
    public function editMajor(Request $request){
        if ($request->isPost()) {
            $major = Major::find($request->param('majorID'));
            if ($major->save(
                [
                    'majorName' => $request->param('majorName'),
                ]
            )) {
                return resultSuccess();
            } else {
                return resultError();
            }
        } else {
            return resultError();
        }
    }
}
